package cn.tedu.sp06.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.web.util.JsonResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@Slf4j
public class RibbonController {
    @Autowired
    private RestTemplate rt;


    @GetMapping("/item-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getItemsFB")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        //调用远程服务的商品服务，获得订单的商品列表
        //{1}-RestTemplate自己定义的一种占位符格式
        /**
         * 注册中心注册表：
         * item-service---localhost：8001，localhost：8002
         * 从注册表表获取
         */
      return   rt.getForObject("http://item-service/{1}",
                JsonResult.class,
                orderId);  //用orderId进行填充占位符{1}
    }
    @PostMapping("/item-service/decreaseNumber")
    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    public JsonResult<?> decreaseNumber(List<Item> item){
        return rt.postForObject("http://item-service/decreaseNumber",
                item, JsonResult.class);
    }

    @GetMapping("/user-service/{userId}")
  //  @HystrixCommand(fallbackMethod = "gerUserFB")
    public JsonResult<User> gerUser(@PathVariable(value = "userId"
    ) Integer userId){
        return rt.getForObject("http://user-service/{userId}",
                JsonResult.class,
                userId);
    }
    @GetMapping("/user-service/{userId}/{score}")
    @HystrixCommand(fallbackMethod = "addScoreFB")
    public JsonResult<?> addScore(@PathVariable(value = "userId") Integer userId,@PathVariable(value = "score") Integer score){
        return rt.getForObject("http://user-service/{1}/score={2}",
                JsonResult.class,
                userId,score);
    }
    @GetMapping("/order-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getOrderFB")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return rt.getForObject("http://order-service/{1}",
                JsonResult.class,
                orderId);
    }
    @GetMapping("/order-service")
    @HystrixCommand(fallbackMethod = "addOrderFB")
    public JsonResult<?> addOrder(){
        return rt.getForObject("http://order-service/",
                JsonResult.class);
    }




    public JsonResult<List<Item>> getItemsFB( String orderId){
       return JsonResult.err("获取订单商品列表失败");
    }

    public JsonResult<?> decreaseNumberFB(List<Item> item){
        return JsonResult.err("减少商品库存失败");
    }


    public JsonResult<User> gerUserFB(Integer userId){
        return JsonResult.err("获取用户失败");
    }


    public JsonResult<?> addScoreFB( Integer userId,Integer score){
        return JsonResult.err("增加用户积分失败");
    }

    public JsonResult<Order> getOrderFB( String orderId){
        return JsonResult.err("获取订单失败");
    }

    public JsonResult<?> addOrderFB(){
        return JsonResult.err("添加订单失败");
    }
}
